package com.example.tiku7;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;

import java.util.List;

public class Recy_Adapter extends RecyclerView.Adapter {
   private List<Bean.ResultsBean>list;
    private  Context context;

    public Recy_Adapter(List<Bean.ResultsBean> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType==1){
            View inflate = LayoutInflater.from(context).inflate(R.layout.item1, parent, false);
            return new ViewHolder1(inflate);
        }else if(viewType==2){
            View inflate = LayoutInflater.from(context).inflate(R.layout.item2, parent, false);
            return new ViewHolder2(inflate);
        }else {
            View inflate = LayoutInflater.from(context).inflate(R.layout.item3, parent, false);
            return new ViewHolder3(inflate);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        int viewType = getItemViewType(position);
        if(viewType==1){
            ViewHolder1 viewHolder1= (ViewHolder1) holder;
            viewHolder1.tv.setText(list.get(position).getPublishedAt()+"");
            Glide.with(context).load(list.get(position).getUrl_1()).into(viewHolder1.img);
        }else if(viewType==2){
            ViewHolder2 viewHolder2= (ViewHolder2) holder;
            Glide.with(context).load(list.get(position).getUrl_1()).into(viewHolder2.img1);
            Glide.with(context).load(list.get(position).getUrl_1()).into(viewHolder2.img2);
            Glide.with(context).load(list.get(position).getUrl_3()).into(viewHolder2.img3);

        }else {
            ViewHolder3 viewHolder3= (ViewHolder3) holder;
            Glide.with(context).load(list.get(position).getUrl_1()).into(viewHolder3.tv_img);
            Glide.with(context).load(list.get(position).getUrl_1()).into(viewHolder3.tv_img2);
        }

    }

    @Override
    public int getItemViewType(int position) {
        if (position%3==1) {
            return 1;
        } else if (position%3 == 2) {
            return 2;
        } else {
            return 3;

        }
    }
    @Override
    public int getItemCount() {
        return list.size();
    }
    private class ViewHolder1 extends RecyclerView.ViewHolder{
            ImageView img;
            TextView tv;

        public ViewHolder1(@NonNull View itemView) {
            super(itemView);
            img=itemView.findViewById(R.id.view_img);
            tv=itemView.findViewById(R.id.tv);
        }
    }
    private class ViewHolder2 extends RecyclerView.ViewHolder{
        ImageView img1;
        ImageView img2;
        ImageView img3;
        public ViewHolder2(@NonNull View itemView) {
            super(itemView);
            img1=itemView.findViewById(R.id.img_1);
            img2=itemView.findViewById(R.id.img_2);
            img3=itemView.findViewById(R.id.img_3);

        }
    }
    private class ViewHolder3 extends RecyclerView.ViewHolder{
        ImageView tv_img;
        ImageView tv_img2;
        public ViewHolder3(@NonNull View itemView) {
            super(itemView);
            tv_img=itemView.findViewById(R.id.img_iv1);
            tv_img2=itemView.findViewById(R.id.img_iv2);

        }
    }
}
